﻿using System;
using System.Collections;
using System.Linq;

namespace MobiSms.Library.Extensions
{
    public static class CollectionExtensions
    {
        /// <summary>
        /// Determines whether the specified collection is null or empty.
        /// </summary>
        /// <param name="self">The collection.</param>
        /// <returns>
        /// <c>true</c> if the specified collection is null or empty; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsNullOrEmpty(this IEnumerable self)
        {
            return (self == null) || (!self.OfType<object>().Any());
        }

        /// <summary>
        /// Determines whether the specified index is within collection boundaries.
        /// </summary>
        /// <param name="self">The collection.</param>
        /// <param name="index">The index to check.</param>
        /// <returns><c>true</c> if index is inside collection boundaries.</returns>
        public static bool IsValidIndex(this ICollection self, int index)
        {
            if (self == null)
                throw new ArgumentNullException("self");

            return index >= 0 && index < self.Count;
        }
    }
}
